package org.smarthings.impl;

import java.util.Properties;

import org.smarthings.Message;
import org.smarthings.MessageBroker;
import org.smarthings.Response;
import org.smarthings.Utils;

public class HttpBrokerImpl extends MessageBroker {
	private static final HttpBrokerImpl instance = new HttpBrokerImpl();
	private String baseUrl;

	private HttpBrokerImpl() {
		Properties configuration = Utils.getConfiguration(this, PROTOCOL.toLowerCase() + ".properties");
		String host = configuration.getProperty("host", "localhost");
		String port = configuration.getProperty("port", "8080");
		baseUrl = "http://" + host + ":" + port + "/";
	}

	public static MessageBroker getInstance() {
		return instance;
	}

	public Message get(String uri, String contentType) {
		Message message = Utils.httpGET(baseUrl + uri, contentType);
		if (message != null) {
			message.setProtocol(PROTOCOL);
		}
		return message;
	}

	public Response post(Message message) {
		return Utils.httpPOST(baseUrl + message.getUri(), message);
	}
}